package com.project.class4_6;

import java.util.Hashtable;
import java.util.Vector;

/**
 * 银行帐号类
 */
public class BankCode {
    /**帐号余额*/
    private int money = 2000;

    public synchronized void test(){

    }

    public void foo(){

    }

    /**
     * 取款
     * @param m 取款金额
     */
    public  synchronized void getMoney(int m){
        if(m > money){
            System.out.println("余额不足");
        }
        else{
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }

            this.money -= m;
            System.out.println("取走："+m);
            System.out.println("余额："+this.money);
        }
    }

    public static void main(String[] args) {
        BankCode bankCode = new BankCode();
        User user1 = new User(bankCode,1300);
        User user2 = new User(bankCode,1500);
        Hashtable a;
        Vector v;
        StringBuffer s;
    }
}

class User implements Runnable{
    /**银行帐号*/
    private BankCode bankCode;
    /**取款金额*/
    private int takeMoney;
    public User(BankCode bankCode,int takeMoney){
        this.bankCode = bankCode;
        this.takeMoney = takeMoney;
        new Thread(this).start();
    }

    @Override
    public void run() {
        this.bankCode.getMoney(takeMoney);
    }
}